﻿CREATE PROCEDURE [ServiceEngine].[UpdateSystemInstance]
	@SystemID uniqueidentifier = NULL,
	@Name nvarchar(400),	
	@OwnerName nvarchar(200) = NULL,
	@Description ntext = null,	
	@OwnerContact nvarchar(200) = NULL,
	@URI nvarchar(500) = NULL,
	@PolicyID uniqueidentifier = NULL,
	@Port int = NULL,
	@ID uniqueidentifier
AS
BEGIN
	DECLARE @ErrorMessage NVARCHAR(4000);
	DECLARE @ErrorSeverity INT;
	DECLARE @ErrorState INT;
	SET NOCOUNT ON;

	BEGIN TRY
		BEGIN TRANSACTION

		UPDATE	ServiceEngine.SystemInstances 
		SET		SystemID = @SystemID,
				OwnerName = @OwnerName,
				OwnerContact = @OwnerContact,
				URI = @URI,
				PolicyID = @PolicyID,
				Port = @Port
		WHERE	InstanceID = @ID

		UPDATE	ServiceEngine.Names
		SET		EntityName = @Name,
				EntityDescription = 	@Description
		WHERE	EntityID = @ID and EntityTypeID = 14

		COMMIT TRANSACTION
	END TRY
	BEGIN CATCH
		SELECT 
			@ErrorMessage = ERROR_MESSAGE(),
			@ErrorSeverity = ERROR_SEVERITY(),
			@ErrorState = ERROR_STATE();

		IF XACT_STATE() <> 0 ROLLBACK TRANSACTION;

		RAISERROR (@ErrorMessage, 
				   @ErrorSeverity,
				   1);
	END CATCH

END